<?php

namespace App\Http\Controllers\Admin;

use App\Theme;
use App\Writing;
use App\WritingComment;
use App\WritingLike;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class WritingController extends Controller
{

    public function index(Request $request){
        $searchKey=$request->searchKey;
        $searchTime=$request->searchTime;
        $category_id=$request->category_id;
        $is_display=$request->is_display;
        return view('admin.writing.list',compact('searchKey','searchTime','category_id','is_display'));
    }
    public function ajax_list(Request $request){

        $model=Writing::with(['user','theme','like']);

        if($request->searchKey){
            $model=$model->where('name','like', '%'.$request->searchKey.'%');
        }

        if($request->category_id&&$request->category_id!=''){
            $model=$model->where('category_id','=',$request->category_id);
        }
        if($request->is_display&&$request->is_display!=''){
            $model=$model->where('is_display','=',$request->is_display);
        }
        if($request->theme_id&&$request->theme_id!=''){
            $model=$model->where('theme_id','=',$request->theme_id);
        }
        if($request->searchTime&&$request->searchTime!=''){
            $timeArray=explode('~',$request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }

        $model=$model->paginate($request->limit)
            ->toArray();

        return $this->page_json_render($model);
    }
    public function ajax_get_theme(Request $request){
        $model=Theme::where('category_id',$request->category_id)->get();
        return view('admin.writing.getTheme',compact('model'));
    }
    //禁止作品显示
    public function display(Request $request){
        $ids=$request->id;
        $model=new Writing();
        if(is_array($ids)){
            $model=$model->whereIn('id',$ids);
        }elseif(is_string($ids)){
            $model=$model->where('id',$ids);
        }
        $model=$model->update([
            'is_display'=>$request->display
        ]);
        return $this->edit_success();

    }
/*    //创建
    public function create(Request $request){
        return view('admin.theme.edit_add');
    }
    //储存
    public function store(Request $request){
        $name=$request->name;
        $this->filter($request);
        $theme=new Theme();
        $theme->name=$request->name;
        $theme->save();
        return redirect()->route('admin.theme_index')->with(
            $this->success()
        );
    }*/
    //编辑
    public function edit(Request $request,$id){
        $model=Writing::findOrFail($id);
        $theme=Theme::all();
        return view('admin.writing.edit_add',compact('id','model','theme'));
    }

    public function update(Request $request){
        $id=$request->id;
        $this->filter($request);
        $model=Writing::findOrFail($id);
        $model->name=$request->name;
        $model->desc=$request->desc;
        $model->category_id=0;
        $model->theme_id=$request->theme_id ? $request->theme_id : $request->theme ;
        $model->thumb_img=$request->thumb_img;
        $model->master=[$request->master1,$request->master2,$request->master3];
        $model->save();
        return redirect()->route('admin.writing_index')->with(
            $this->success()
        );
    }
    public function del(Request $request){
        $ids=$request->id;

        $model=Writing::destroy($ids);


        WritingComment::whereIn('writing_id',[$ids])->delete();
        WritingLike::whereIn('writing_id',[$ids])->delete();
        return $this->del_success();

    }

    protected function filter($request){
        $this->validate($request,
            [
                'name'=>'required',
            ]);
    }

}
